package com.jeecg.gleave.service.impl;
import com.jeecg.gleave.service.GLeaveServiceI;
import com.jeecg.student.entity.GStudentEntity;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;

import com.jeecg.common.Assert;
import com.jeecg.gleave.entity.GLeaveEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;

@Service("gLeaveService")
@Transactional
public class GLeaveServiceImpl extends CommonServiceImpl implements GLeaveServiceI {

	
 	public void delete(GLeaveEntity entity) throws Exception{
 		super.delete(entity);
 		//执行删除操作增强业务
		this.doDelBus(entity);
 	}
 	
 	public Serializable save(GLeaveEntity entity) throws Exception{
 		Serializable t = super.save(entity);
 		//执行新增操作增强业务
 		this.doAddBus(entity);
 		return t;
 	}
 	
 	public void saveOrUpdate(GLeaveEntity entity) throws Exception{
 		super.saveOrUpdate(entity);
 		//执行更新操作增强业务
 		this.doUpdateBus(entity);
 	}
 	
 	/**
	 * 新增操作增强业务
	 * @param t
	 * @return
	 */
	private void doAddBus(GLeaveEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 更新操作增强业务
	 * @param t
	 * @return
	 */
	private void doUpdateBus(GLeaveEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 删除操作增强业务
	 * @param id
	 * @return
	 */
	private void doDelBus(GLeaveEntity t) throws Exception{
	    //-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	
 	private Map<String,Object> populationMap(GLeaveEntity t){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", t.getId());
		map.put("le_name", t.getLeName());
		map.put("le_num", t.getLeNum());
		map.put("le_class", t.getLeClass());
		map.put("le_cause", t.getLeCause());
		map.put("le_start", t.getLeStart());
		map.put("le_end", t.getLeEnd());
		map.put("ap_name", t.getApName());
		map.put("ap_date", t.getApDate());
		map.put("ap_result", t.getApResult());
		return map;
	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @param t
	 * @return
	 */
 	public String replaceVal(String sql,GLeaveEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{le_name}",String.valueOf(t.getLeName()));
 		sql  = sql.replace("#{le_num}",String.valueOf(t.getLeNum()));
 		sql  = sql.replace("#{le_class}",String.valueOf(t.getLeClass()));
 		sql  = sql.replace("#{le_cause}",String.valueOf(t.getLeCause()));
 		sql  = sql.replace("#{le_start}",String.valueOf(t.getLeStart()));
 		sql  = sql.replace("#{le_end}",String.valueOf(t.getLeEnd()));
 		sql  = sql.replace("#{ap_name}",String.valueOf(t.getApName()));
 		sql  = sql.replace("#{ap_date}",String.valueOf(t.getApDate()));
 		sql  = sql.replace("#{ap_result}",String.valueOf(t.getApResult()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	/**
	 * 执行JAVA增强
	 */
 	private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map<String,Object> data) throws Exception {
 		if(StringUtil.isNotEmpty(cgJavaValue)){
			Object obj = null;
			try {
				if("class".equals(cgJavaType)){
					//因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
					obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
				}else if("spring".equals(cgJavaType)){
					obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
				}
				if(obj instanceof CgformEnhanceJavaInter){
					CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
					javaInter.execute("g_leave",data);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("执行JAVA增强出现异常！");
			} 
		}
 	}

	@Override
	public GLeaveEntity getLeave(String id) throws Exception {
		Assert.assertArgumentNotEmpty(id, "id");
		
	    String hql = "from " + GLeaveEntity.class.getName() + " where id = ?";
	    List<GLeaveEntity> results = findHql(hql, id);
	    return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public Integer getLeaveCount(String state) throws Exception {
		
	    String hql = "from " + GLeaveEntity.class.getName() + " where apResult = ?";
	    List<GLeaveEntity> results = findHql(hql, state);
	    return results.isEmpty() ? 0 : results.size();
	}
}